package com.puman.beautyfarmcounselor.ui.customer;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.core.BaseInferface;
import com.puman.beautyfarmcounselor.ui.home.adpter.SurplusListAdapter;
import com.puman.beautyfarmcounselor.ui.customer.model.Surplus;
import com.puman.beautyfarmcounselor.util.XTHttp;
import com.puman.beautyfarmcounselor.util.config.RequestAddr;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.List;

/**
 * Created by Curry on 2015/7/2.
 * 院余提醒
 */
@EFragment(R.layout.surplus_layout)
public class SurplusFragment extends BaseFragment implements BaseInferface {

    /**
     * init UI
     */
    @ViewById(R.id.back_btn)
    ImageView backBtn;
    @ViewById(R.id.base_title)
    TextView title;
    @ViewById(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @ViewsById({R.id.grand_total_content, R.id.month_overdue_content, R.id.datetime_content})
    List<TextView> detail;
    @FragmentArg
    int customerId;
    /**
     * init variables
     */
    SurplusListAdapter surplusListAdapter;


    @Override
    @AfterViews
    public void updateView() {
        title.setText(R.string.surplus_title);
        backBtn.setVisibility(View.VISIBLE);
        initView();
    }

    void initView() {
        /**
         * 设置layoutmanager
         */
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);

        getData();

    }


    XTHttp xtHttp = new XTHttp();
    String url = "";
    String param = "";

    private void getData() {
        param = "customer_id=" + customerId;
        url = RequestAddr.surplusRecord + param;
        xtHttp.get(url, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    Surplus surplus;

    private void dealData(Object o) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            surplus = mapper.readValue(o.toString(), Surplus.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (surplus == null)
            return;
        if (surplus.getData().getSurplus_list() == null)
            return;
        detail.get(0).setText(surplus.getData().getTotal_count() + "");
        detail.get(1).setText(surplus.getData().getWill_overdue() + "");
        detail.get(2).setText(surplus.getData().getLast_end_date() + "");
        if (surplus.getData().getSurplus_list().size() == 0)
            mRecyclerView.setVisibility(View.GONE);

        surplusListAdapter = new SurplusListAdapter(getActivity(), surplus.getData().getSurplus_list());
        mRecyclerView.setAdapter(surplusListAdapter);
        surplusListAdapter.notifyDataSetChanged();

    }

    @Click({R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
